package com.example.demo.controller;

import com.example.demo.model.activiti.Task;
import com.example.demo.service.IDemoService;
import com.example.demo.service.IWorkFlowService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("demo")
public class DemoControl {
    @Autowired
    private IDemoService demoService;

    @Autowired
    private IWorkFlowService workFlowService;

    @ApiOperation(value = "测试",notes = "")
    @GetMapping("demo01")
    public ResponseEntity demo01(@RequestParam String str){
        return ResponseEntity.ok(demoService.showStr(str));
    }

    /** 【简单】开始一个流程 */
    @ApiOperation(value = "【简单】开始一个流程",notes = "")
    @GetMapping("startWorkFlow")
    public ResponseEntity startWorkFlow(@RequestParam("processDefinitionKey") String processDefinitionKey){
        workFlowService.startProcessInstance(processDefinitionKey);
        return ResponseEntity.ok(true);
    }

    @ApiOperation(value = "测试名为DynamicAssignmentOperator的工作流",notes = "")
    @GetMapping("dynamicAssignmentOperator")
    public ResponseEntity dynamicAssignmentOperator(){
        workFlowService.dynamicAssignmentOperator("DynamicAssignmentOperator");
        return ResponseEntity.ok(true);
    }

    @ApiOperation(value = "获取用户代办业务",notes = "")
    @GetMapping("getUserTasks")
    public ResponseEntity getUserTasks(@RequestParam("assignee") String assignee){
        List<Task> taskList = workFlowService.getUserTasks(assignee);
        return ResponseEntity.ok(taskList);
    }

    /**
     * 【简单】完成任务
     * @param taskId 任务编号
     * @return
     */
    @ApiOperation(value = "【简单】完成任务",notes = "")
    @GetMapping("completePersonTask")
    public ResponseEntity completePersonTask(@RequestParam("taskId") String taskId){
        workFlowService.completePersonTask(taskId);
        return ResponseEntity.ok(true);
    }

    /**
     * 完成任务
     * @param taskId 任务编号
     * @param userId 要传给的用户
     * @return
     */
    @ApiOperation(value = "完成任务",notes = "")
    @GetMapping("completePersonTaskToUser")
    public ResponseEntity completePersonTaskToUser(@RequestParam("taskId") String taskId,
                                                   @RequestParam("userId") String userId){
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("userId", userId); //因为在bpmn文件中是#{userId}，所以key值为userId
        workFlowService.completePersonTask(taskId, variables);
        return ResponseEntity.ok(true);
    }

    /**
     * 分配个人任务从一个人到另一个人（认领任务）
     * @param taskId 任务编号
     * @param userId 要被指定的办理人
     * @return
     */
    @ApiOperation(value = "分配个人任务从一个人到另一个人（认领任务）",notes = "")
    @GetMapping("setAssigneeTask")
    public ResponseEntity setAssigneeTask(@RequestParam("taskId") String taskId,
                                          @RequestParam("userId") String userId){
        workFlowService.setAssigneeTask(taskId, userId);
        return ResponseEntity.ok(true);
    }

}
